/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.progin.drop.core;

import java.io.File;
import java.util.ArrayList;

/**
 *
 * @author HP
 */
public class Message {
    public static final int MESSAGE_LIST_FILES = 1;
    public static final int MESSAGE_REQUEST_FILE = 2;
    public static final int MESSAGE_SEND_FILE = 3;
    //public static final int MESSAGE_RECEIVE_FILES = 4;

    private ArrayList<String> mBodies;
    private ArrayList<String> mHeaders;
    private byte[] mByteStream;
    private File mAttachment = null;

    private Message() {
        mBodies = new ArrayList<String>();
        mHeaders = new ArrayList<String>();
        mByteStream = null;
    }

    public long getAttachmentLength() {
        if (mAttachment != null)
            return mAttachment.length();
        else
            return 0;
    }

    public File getAttachment() {
        return mAttachment;
    }

    public void setAttachment(File f) {
        mAttachment = f;
    }

    public Message(int id , File attachment) {
        this();
        mAttachment = attachment;
        mHeaders.add(String.valueOf(id));
    }

    /*public static Message construct(String stream , byte[] byteStream) throws MessageException {
        Message retval = construct(stream);
        retval.mByteStream = byteStream;
        return retval;
    }*/

    public static Message construct(String stream , File attachment) throws MessageException {
        Message retval = construct(stream);
        retval.mAttachment = attachment;
        return retval;
    }

    public static Message construct(String stream) throws MessageException {
        Message retval = new Message();
        String[] tokens;

        tokens = stream.split("#");

        if (tokens.length < 3) throw new MessageException("Incompatible message format");

        String header = tokens[1];
        String body = tokens[2];

        String[] headers = header.split(";");
        String[] bodies = body.split(";");

        for (String h : headers)
            retval.getmHeaders().add(h);

        for (String b : bodies)
            retval.getmBodies().add(b);

        return retval;
    }

    public int getId() throws MessageException {
        if (mHeaders.size() < 1)
            throw new MessageException("Incompatible message format");
        else
            return Integer.valueOf(mHeaders.get(0));
    }

    public String getHeader(int index) throws MessageException {
        if (index >= mHeaders.size())
            throw new MessageException("Index out of bound");
        else
            return mHeaders.get(index);
    }

    public void setByteStream(byte[] b) {
        mByteStream = b;
    }

    public byte[] getByteStream() {
        return mByteStream;
    }

    public String getBody(int index) throws MessageException {
        if (index >= mBodies.size())
            throw new MessageException("Index out of bound");
        else
            return mBodies.get(index);
    }

    @Override
    public String toString() {
        String retval = "";

        retval += "#";
        for (String h : mHeaders) {
            retval += h + ";";
        }
        if (mHeaders.size() == 0)
            retval += ";";

        retval += "#";
        for (String b : mBodies) {
            retval += b + ";";
        }
        if (mBodies.size() == 0)
            retval += ";";
        retval += "#";

        return retval;
    }

    /**
     * @return the mBodies
     */
    public ArrayList<String> getmBodies() {
        return mBodies;
    }

    /**
     * @param mBodies the mBodies to set
     */
    public void setmBodies(ArrayList<String> mBodies) {
        this.mBodies = mBodies;
    }

    /**
     * @return the mHeaders
     */
    public ArrayList<String> getmHeaders() {
        return mHeaders;
    }

    /**
     * @param mHeaders the mHeaders to set
     */
    public void setmHeaders(ArrayList<String> mHeaders) {
        this.mHeaders = mHeaders;
    }
}
